﻿using DotSpatial.Controls;
using DotSpatial.Data;
using DotSpatial.Symbology;
using DotSpatial.Topology;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KASEM
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void cmdAddShp_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDlg = new OpenFileDialog();
            openFileDlg.Filter = "*.shp|*.shp";
            openFileDlg.ShowDialog();
            if (openFileDlg.FileName.Length > 0)
            {
                kMap.AddLayer(openFileDlg.FileName);
                cmdAddShp.Enabled = false;
            }
        }

        private void cmdZoomIn_Click(object sender, EventArgs e)
        {
            kMap.FunctionMode = FunctionMode.ZoomIn;
        }

        private void cmdZoomOut_Click(object sender, EventArgs e)
        {
            kMap.FunctionMode = FunctionMode.ZoomOut;
        }

        private void cmdZoomExtend_Click(object sender, EventArgs e)
        {
            Envelope env = new Envelope();
            kMap.ViewExtents = kMap.Extent;
        }

        private void cmdPan_Click(object sender, EventArgs e)
        {
            kMap.FunctionMode = FunctionMode.Pan;
        }

        private void cmdInformation_Click(object sender, EventArgs e)
        {
            kMap.FunctionMode = FunctionMode.Info;
        }

        private void cmdOpenFratureDialog_Click(object sender, EventArgs e)
        {

        }

        private void cmdSelection_Click(object sender, EventArgs e)
        {
            kMap.FunctionMode = FunctionMode.Select;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
            for (int x = 0; x < 361; x++)
            {
                int activeRow = grdGeometry.Rows.Add();
                grdGeometry.Rows[activeRow].Cells[0].Value = x;
                for (int j = 1; j < 5;j++ )
                {
                    grdGeometry.Rows[activeRow].Cells[j].Value = Math.Cos(Math.PI * (x+(j-1)*10) / 180) * 100;
                }
            }
        }

        private void cmdCreateline_Click(object sender, EventArgs e)
        {
            kMap.MapFrame.DrawingLayers.Clear();
            int nPt = grdGeometry.RowCount - 1;
            FeatureSet fs = new FeatureSet(FeatureType.Line);
            for (int j = 1; j < 5; j++)
            {
                Coordinate[] cr = new Coordinate[nPt]; //x-axis
                for (int i = 0; i < nPt; i++)
                {
                    Double x1 = Convert.ToDouble(grdGeometry.Rows[i].Cells[0].Value);
                    Double y1 = Convert.ToDouble(grdGeometry.Rows[i].Cells[j].Value);
                    cr[i] = new Coordinate(x1, y1);
                }
                LineString ls = new LineString(cr);
                Feature f = new Feature(ls);
                fs.Features.Add(f);
            }
            MapLineLayer curveShp;
            curveShp = new MapLineLayer(fs);
            curveShp.Symbolizer = new LineSymbolizer(Color.Blue , 1);

            kMap.MapFrame.DrawingLayers.Add(curveShp);

            // Request a redraw
            kMap.MapFrame.Invalidate();

            //------------------------------------------------------------------------------------
            Envelope env = new Envelope();
            env.SetExtents(-100, -100, 500, 100);
            kMap.Extent.SetValues(0,-100,360,100);  

        }
    }
}
